﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Tour_TourTypeList : VST.Controls.BaseLCGPage
{
    public int? TourId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPropertyFromQueryString();
            LoadData();
        }
    }
    void GetPropertyFromQueryString()
    {
        TourId = CommonHelper.QueryStringInt("TourId", -1);
    }
    void GetPropertyFromForm()
    {
        //ParentId = ddlParent.SelectedValue.ToInt();
        //IsVisible = chbVisible.Checked;
    }

    void LoadData()
    {
        grv.DataSource = TblTourImageManager.CreateInstant().SelectTourImageBy(null, TourId, "Id", "DESC");
        grv.DataBind();
    }

    protected void grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var hdfID = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfID");
        var hdfTourId = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfTourId");
        try
        {
            UpdateOrderOffAndDelete(hdfID.Value, hdfTourId.Value, 100);
            TblTourImageManager.CreateInstant().Delete(hdfID.Value.ToInt());
            Response.Redirect(Request.RawUrl);
            Trace.Warn("Delete Success" + hdfID.Value);
        }
        catch (Exception ex)
        {
            Trace.Warn("Delete Error" + hdfID.Value + ex.Message);
        }
        LoadData();
        Response.Redirect(Request.RawUrl);
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            var txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            var hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            var hdfTourId = (HiddenField)grv.Rows[i].FindControl("hdfTourId");
            var hdfID = (HiddenField)grv.Rows[i].FindControl("hdfID");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);
            if (OldIndex != NewIndex)
                UpdateOrderOffAndDelete(hdfID.Value, hdfTourId.Value, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And TourId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And TourId={0}", strParentId);
        }
        string Whereclause1 = String.Format(" Id={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblTourImage", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {//
        GetPropertyFromForm();
        grv.PageIndex = e.NewPageIndex;
        LoadData();
    }
}